package com.atguigu.tms.mock.task;


import com.atguigu.mock.util.*;
import com.atguigu.tms.mock.bean.*;
import com.atguigu.tms.mock.config.AppConfig;
import com.atguigu.tms.mock.constant.TmsConstant;

import com.atguigu.tms.mock.service.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.util.Date;
import java.util.concurrent.atomic.AtomicInteger;

@Component
@Slf4j
public class OrderMockTask {

    @Autowired
    OrderInfoService orderInfoService;

    @Autowired
    BaseComplexService baseComplexService;

    @Autowired
    OrderCargoService orderCargoService;

    @Autowired
    LineBaseInfoService lineBaseInfoService;

    @Autowired
    UserInfoService userInfoService;

    public  void   doOrder(Date curDate  ,Integer curHour, Integer curMinute){
        // 随机生成用户列表
        Integer orderCount = AppConfig.mock_order_count;

        for (int i = 0; i < orderCount; i++) {
            UserInfo userInfo = userInfoService.getOneByRandom();
            userOrder(userInfo, curDate);

        }





    }

    AtomicInteger orderFlowId= new AtomicInteger(0);
    Long batchNo=new Date().getTime()/1000- 612892800;

    public OrderInfo   userOrder(UserInfo userInfo  ,Date curDate){

        OrderInfo orderInfo = new OrderInfo();

        int flowId = orderFlowId.addAndGet(1);
        String flowString = StringUtils.leftPad(flowId + "", 8,"0");
        String orderNo = LocalDate.now().getYear()+"-"+batchNo+"-"+flowString ;

        orderInfo.setOrderNo(orderNo);
        orderInfo.setUserId(userInfo.getId());

        orderInfo.setDistance(BigDecimal.valueOf(1200));

        orderInfo.setCargoNum(RandomNum.getRandInt(1,3));

        orderInfo.setStatus(TmsConstant.ORDER_STATUS_ORDERED);
        orderInfo.setIsDeleted("0");

        UserAddress defaultAddress = userInfo.getDefaultAddress();

        orderInfo.setSenderAddress(defaultAddress.getAddress());
        orderInfo.setSenderCityId(defaultAddress.getCityId());
        orderInfo.setSenderComplexId(defaultAddress.getComplexId());
        orderInfo.setSenderDistrictId(defaultAddress.getDistrictId());
        orderInfo.setSenderProvinceId( defaultAddress.getProvinceId());
        orderInfo.setSenderPhone(userInfo.getPhoneNum());
        orderInfo.setSenderName(userInfo.getRealName());


        BaseComplex baseComplex = baseComplexService.getOneByRandom();

        orderInfo.setCollectType(TmsConstant.COLLECT_TYPE_COURIER);

        orderInfo.setReceiverProvinceId(baseComplex.getProvinceId());
        orderInfo.setReceiverCityId(baseComplex.getCityId());
        orderInfo.setReceiverDistrictId(baseComplex.getDistrictId());
        orderInfo.setReceiverComplexId(baseComplex.getId());
        orderInfo.setReceiverAddress(baseComplex.getDistrictName()+baseComplex.getComplexName()+ RandomNum.getRandInt(1,3)+"号楼"+ RandomNumString.getRandNumString(1,5,3,"",true));
        orderInfo.setReceiverName(RandomName.genName());
        orderInfo.setReceiveLocation("32.123123,71.12341");
        orderInfo.setReceiverPhone(  "13"+ RandomNumString.getRandNumString(1,9,9,"") );

        orderInfo.setCreateTime(curDate);
        OrderCargo orderCargo = genOrderCargo(orderInfo);

        //计算距离
        Long distance=0L;

        if(!orderInfo.getSenderDistrictId().equals(orderInfo.getReceiverDistrictId())){
            distance = lineBaseInfoService.caleLineInfoDistance(orderInfo.getSenderDistrictId(), orderInfo.getReceiverDistrictId());
        }
        orderInfo.setDistance(BigDecimal.valueOf(distance+RandomNum.getRandInt(10,20)));


        BigDecimal estimatePrice = estimatePrice(orderInfo.getDistance(), orderCargo);
        orderInfo.setAmount(estimatePrice);


        Date arriveTime = estimateArriveTime(curDate, orderInfo.getDistance());
        orderInfo.setEstimateArriveTime(arriveTime);

        orderInfoService.saveOrUpdate(orderInfo,true);

        orderCargo.setOrderId(orderInfo.getId());

        orderCargoService.saveOrUpdate(orderCargo,true);

        return orderInfo;

    }


    public OrderCargo genOrderCargo(OrderInfo orderInfo){

        RandomBox cargoTypeBox = new RandomBox(TmsConstant.CARGO_TYPE_DAILY,
                TmsConstant.CARGO_TYPE_FOOD, TmsConstant.CARGO_TYPE_FILE,
                TmsConstant.CARGO_TYPE_CLOTHER, TmsConstant.CARGO_TYPE_DEVICE,
                TmsConstant.CARGO_TYPE_OTHER);
        OrderCargo orderCargo = new OrderCargo();

        RandomBox  ifTooBigBox = new RandomBox(2, 8);



        orderCargo.setWeight(BigDecimal.valueOf(RandomNum.getRandInt(1, 10)) );
        if (ifTooBigBox.getRandBoolValue()){
            orderCargo.setVolumeLength(RandomNum.getRandInt(15, 30));
            orderCargo.setVolumeWidth(RandomNum.getRandInt(15, 30));
            orderCargo.setVolumeHeight(RandomNum.getRandInt(15, 30));
        }
        orderCargo.setCargoType(cargoTypeBox.getRandStringValue());
        orderCargo.setIsDeleted("0");
        orderCargo.setCreateTime(orderInfo.getCreateTime());



        return orderCargo;

    }


    //每100公里n个小时
    private  Date  estimateArriveTime(Date curDate,BigDecimal distance){
        BigDecimal durHour = distance.divide(BigDecimal.valueOf(100), RoundingMode.UP).multiply(BigDecimal.valueOf(AppConfig.order_dur_hour_per100m));
        Date arriveTime = MyDateUtil.addHours(curDate, durHour.intValue(), true);
        return arriveTime;
    }


    private BigDecimal estimatePrice(BigDecimal distance, OrderCargo orderCargo){

        BigDecimal  weightValue=orderCargo.getWeight();

        if(orderCargo.getVolumeLength()!=null &&
                ! orderCargo.getVolumeLength().equals(BigDecimal.ZERO)){
            BigDecimal volume= BigDecimal.valueOf(orderCargo.getVolumeHeight() * orderCargo.getVolumeLength() * orderCargo.getVolumeWidth());

            BigDecimal  volumeWeightValue =  volume.divide(BigDecimal.valueOf(8000), RoundingMode.UP);

            if(weightValue.compareTo(volumeWeightValue)<0){
                weightValue=volumeWeightValue;
            }
        }

        BigDecimal basePrice=BigDecimal.ZERO;
        BigDecimal exPrice=BigDecimal.ZERO;
        if(distance.compareTo(BigDecimal.valueOf(300))>0){
            basePrice=BigDecimal.valueOf(10);
            exPrice=BigDecimal.valueOf(1);
        }else{
            basePrice=BigDecimal.valueOf(12);
            exPrice=distance.divide(BigDecimal.valueOf(500),RoundingMode.UP);
        }

        BigDecimal price=basePrice.add( exPrice.multiply(weightValue.subtract(BigDecimal.ONE)));

        return price;


    }


    public static void main(String[] args) {
        if(152L==152L){
            System.out.println("true");
        }
    }

}
